////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Michiel Helvensteijn                                    //
//                                                                            //
// This file is part of the Mist compiler.                                    //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modif   //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// along with the Mist compiler.  If not, see <http://www.gnu.org/licenses/>. //
////////////////////////////////////////////////////////////////////////////////

#include "codeposition.hpp"

/******************************************************************************
 * Includes                                                                   *
 ******************************************************************************/

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
using boost::make_tuple;

#include "position.hh"

/******************************************************************************
 * Function Implementations                                                   *
 ******************************************************************************/

CodePosition::CodePosition(const string& file, unsigned int line, unsigned int column)
	: _file(file), _line(line), _column(column) {
} // intentionally empty

CodePosition::CodePosition(const yy::position& pos)
	: _file(*pos.filename), _line(pos.line), _column(pos.column + 1) {
} // intentionally empty

void CodePosition::set_file(const string& file) {
	_file = file;
}

const string& CodePosition::file() const {
	return _file;
}

void CodePosition::set_line(unsigned int line) {
	_line = line;
}

unsigned int CodePosition::line() const {
	return _line;
}

void CodePosition::set_column(unsigned int column) {
	_column = column;
}

unsigned int CodePosition::column() const {
	return _column;
}

bool CodePosition::operator==(const CodePosition& other) const {
	return file()      == other.file()
	       && line()   == other.line()
	       && column() == other.column();
}

bool CodePosition::operator<(const CodePosition& other) const {
	return file() == other.file()
	    &&   make_tuple(      line(),       column())
	       < make_tuple(other.line(), other.column());
}

ostream& CodePosition::report_to_stream(ostream& stream) const {
	if (!file().empty())
		stream << file() << ":";
	stream << line() << ":" << column();
	
	return stream;
}

ostream& operator<<(ostream& stream, const CodePosition& pos) {
	return pos.report_to_stream(stream);
}
